package com.example.sunmiv1.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.RefuseDetailAdapter;
import com.example.sunmiv1.entity.RefuseDetailInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefuseDetailActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.refuseDetail_listView)
    ListView refuseDetailListView;

    private Context context;
    private LoadingDialog loadingDialog;
    private ACache aCache;
    private RefuseDetailAdapter refuseDetailAdapter;
    private int flag = 0;//1:GoodWarehousingActivity 2:AllocationAdapter
    private RefuseDetailInfo refuseDetailInfo;
    private List<RefuseDetailInfo.DataBean.ListBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_refuse_detail);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.refuseDetailTile);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        flag = getIntent().getIntExtra("flag", 0);
        JSONObject jsonObject = new JSONObject();
        try {
            if (flag == 1) {//門店入庫詳情
                jsonObject.put("stock_out_id", getIntent().getStringExtra("stock_out_id"));
                jsonObject.put("type", getIntent().getStringExtra("type"));
            } else if (flag == 2) {//門店出庫詳情
                jsonObject.put("stock_out_id", getIntent().getStringExtra("stock_out_id"));
                jsonObject.put("user_type",getIntent().getStringExtra("userType"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        getRefuseDetail(jsonObject);
    }

    private void getRefuseDetail(JSONObject jsonObject) {
        loadingDialog.show();
        jsonObject = Tools.createParameterCode(jsonObject);
        HttpUtils.post(context, Common.STORE_REFUSE_INFO, jsonObject, new TextCallBack() {
            @Override
            protected void onSuccess(String text) {
                loadingDialog.dismiss();
                refuseDetailInfo = GsonUtils.JsonToBean(text, RefuseDetailInfo.class);
                if (refuseDetailInfo.getStatus() == 1) {
                    list.addAll(refuseDetailInfo.getData().getList());
                    setRefuseDetailAdapter();
                }
            }

            @Override
            protected void onFailure(ResponseException e) {
                loadingDialog.dismiss();
            }
        });
    }

    private void setRefuseDetailAdapter() {
        refuseDetailAdapter = new RefuseDetailAdapter(list);
        refuseDetailListView.setAdapter(refuseDetailAdapter);
    }


    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }
}
